/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package test.controllers;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import test.controllers.exceptions.NonexistentEntityException;
import test.controllers.exceptions.PreexistingEntityException;
import test.entities.Action;
import test.entities.HistoriqueAction;

/**
 *
 * @author Issam
 */
public class HistoriqueActionJpaController implements Serializable {

    public HistoriqueActionJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(HistoriqueAction historiqueAction) throws PreexistingEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Action numAction = historiqueAction.getNumAction();
            if (numAction != null) {
                numAction = em.getReference(numAction.getClass(), numAction.getNumAction());
                historiqueAction.setNumAction(numAction);
            }
            em.persist(historiqueAction);
            if (numAction != null) {
                numAction.getHistoriqueActionCollection().add(historiqueAction);
                numAction = em.merge(numAction);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findHistoriqueAction(historiqueAction.getIdHistAction()) != null) {
                throw new PreexistingEntityException("HistoriqueAction " + historiqueAction + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(HistoriqueAction historiqueAction) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            HistoriqueAction persistentHistoriqueAction = em.find(HistoriqueAction.class, historiqueAction.getIdHistAction());
            Action numActionOld = persistentHistoriqueAction.getNumAction();
            Action numActionNew = historiqueAction.getNumAction();
            if (numActionNew != null) {
                numActionNew = em.getReference(numActionNew.getClass(), numActionNew.getNumAction());
                historiqueAction.setNumAction(numActionNew);
            }
            historiqueAction = em.merge(historiqueAction);
            if (numActionOld != null && !numActionOld.equals(numActionNew)) {
                numActionOld.getHistoriqueActionCollection().remove(historiqueAction);
                numActionOld = em.merge(numActionOld);
            }
            if (numActionNew != null && !numActionNew.equals(numActionOld)) {
                numActionNew.getHistoriqueActionCollection().add(historiqueAction);
                numActionNew = em.merge(numActionNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = historiqueAction.getIdHistAction();
                if (findHistoriqueAction(id) == null) {
                    throw new NonexistentEntityException("The historiqueAction with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            HistoriqueAction historiqueAction;
            try {
                historiqueAction = em.getReference(HistoriqueAction.class, id);
                historiqueAction.getIdHistAction();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The historiqueAction with id " + id + " no longer exists.", enfe);
            }
            Action numAction = historiqueAction.getNumAction();
            if (numAction != null) {
                numAction.getHistoriqueActionCollection().remove(historiqueAction);
                numAction = em.merge(numAction);
            }
            em.remove(historiqueAction);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<HistoriqueAction> findHistoriqueActionEntities() {
        return findHistoriqueActionEntities(true, -1, -1);
    }

    public List<HistoriqueAction> findparDate ()
    {
        List<HistoriqueAction> result = new ArrayList<HistoriqueAction>(); 
        EntityManager em = getEntityManager();    
        CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
        cq.select(cq.from(HistoriqueAction.class));
        Query q = em.createQuery(cq+"order by h.date");
        result = q.getResultList() ; 
        return result ;
       
    }
    
    public List<HistoriqueAction> findHistoriqueActionEntities(int maxResults, int firstResult) {
        
     
        
        return findHistoriqueActionEntities(false, maxResults, firstResult);
    }

    private List<HistoriqueAction> findHistoriqueActionEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(HistoriqueAction.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public HistoriqueAction findHistoriqueAction(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(HistoriqueAction.class, id);
        } finally {
            em.close();
        }
    }

    public int getHistoriqueActionCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<HistoriqueAction> rt = cq.from(HistoriqueAction.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
